package com.lizhaoshan.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.lizhaoshan.common.TokenHolder;
import com.lizhaoshan.dao.EmployeeDao;
import com.lizhaoshan.dto.EmployeeDto;
import com.lizhaoshan.pojo.Employee;
import com.lizhaoshan.pojo.PageBean;
import com.lizhaoshan.service.EmployeeService;
import com.lizhaoshan.vo.EmployeeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * @program: reggie
 * @description:
 * @author: lamour
 * @create: 2022-05-14 17:19
 **/

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;


    @Override
    public boolean insertEmployee(EmployeeDto employeeDto) {

        //因为是添加操作，所以创建者和更新者是同一个人
        String currentId = TokenHolder.getCurrentId();
        employeeDto.setCreateUser(currentId);
        employeeDto.setUpdateUser(currentId);
        // String idStr = new Snowflake().nextIdStr();
        // employeeDto.setCreateUser(idStr);
        // employeeDto.setUpdateUser(idStr);

        //设置员工id
        String newId = IdWorker.getIdStr();
        employeeDto.setId(newId);

        //设置员工初始密码，自己不能设置密码吗？
        employeeDto.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //设置操作时间
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //因为员工的CreateTime属性类型是String，所以要转换一下，当时应该设置为Date类型
        String time = dtf.format(LocalDateTime.now());
        employeeDto.setCreateTime(time);
        employeeDto.setUpdateTime(time);
        employeeDto.setStatus(1);

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDto,employee);


        return employeeDao.insertEmployee(employee) > 0 ;
    }

    @Override
    public boolean updateEmployee(EmployeeDto employeeDto) {

        // String currentId = TokenHolder.getCurrentId();
        // employeeDto.setUpdateUser(currentId);
        String idStr = new Snowflake().nextIdStr();
        employeeDto.setUpdateUser(idStr);
        //设置修改时间
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String time = dtf.format(LocalDateTime.now());
        employeeDto.setUpdateTime(time);
        employeeDto.setCreateTime(time);

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDto,employee);

        return employeeDao.updateEmployee(employee) > 0;
    }


    @Override
    public Employee selectByCondition(EmployeeDto employeeDto) {

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDto, employee);

        return employeeDao.selectByCondition(employee);
    }

    @Override
    public PageBean<EmployeeVo> selectPage(Integer page, Integer pageSize, String name) {
        //对传入的数据处理
        int begin=(page-1)*pageSize;
        int size=pageSize;

        if (name != null && name.length() > 0) {
            name="%"+name+"%";
        }
        log.info("********分页查询员工表，begin");
        List<EmployeeVo> employees = employeeDao.selectPage(begin, size,name);
        int count = employeeDao.countTotal();
        log.info("********分页查询员工表，begin");
        if (CollUtil.isEmpty(employees)) {
            return null;
        }
        //封装页面信息
        PageBean<EmployeeVo> employeePageBean = new PageBean<>();
        employeePageBean.setRecords(employees);
        employeePageBean.setTotal(count);
        return employeePageBean;
    }

    @Override
    public Employee selectById(String id) {

        return employeeDao.selectById(id);
    }
}
